package com.example.demo.controller;

import com.example.demo.DTO.ResultDTO;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/18 11:06
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户注册登录登出")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping(value = "/register")
    @ApiOperation("注册")
    public Object register(User user) {
        ResultDTO resultDTO = userService.register(user);
        return resultDTO;
    }
    @ApiOperation("登录")
    @PostMapping(value = "/login")
    public Object login(@RequestParam("email") String email, @RequestParam("password")String password) {
        ResultDTO resultDTO = userService.login(email,password);
        return resultDTO;
    }
    @ApiOperation("注销")
    @GetMapping("/logout")
    public void logout(ServletRequest request){
        HttpServletRequest req = (HttpServletRequest) request;
        String token=req.getHeader("token");
        userService.logout(token);
    }
}

